package HoTan.global.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;

public class  OperateFile{
	

	/**
	 * Doc noi dung file tren tung dong
	 * @param filename
	 * @return
	 * @throws IOException
	 */
	public String readLines(String filename) throws IOException{
		File f = new File(filename);
		FileReader fr	= new FileReader(f);
		BufferedReader br = new BufferedReader(fr);
		String content = "";
		String line = "";
		while((line = br.readLine()) != null){
			content+=line+"\n";
		}
		br.close();
		return content;
	}
	/**
	 * Doc noi dung file thanh mot xau
	 * @param filename
	 * @return
	 * @throws IOException
	 */
	public String readStringFile(String filename) throws IOException{
		FileInputStream fin = new FileInputStream(filename);
		Reader r= new InputStreamReader(fin, "UTF8");
		BufferedReader br = new BufferedReader(r);
		StringBuilder text = new StringBuilder();
		String line =null;
		
		while ((line = br.readLine())!= null){
			
			text.append(line+"\n");
			
		}
		String str =text.toString();
		br.close();
		return str;
	}
	/**
	 * ghi noi dung vao file
	 * @param filename
	 * @param xau
	 * @throws IOException
	 */
	public	void writeFile(String filename,String xau) throws IOException{
		FileOutputStream fout = new FileOutputStream(filename);
		Writer out = new OutputStreamWriter(fout,"UTF8");
		out.write(xau);
		out.close();
		fout.close();
	}
	/**
	 * Doc file dang ma tran
	 * @param filename
	 * @param column
	 * @param row
	 * @return
	 * @throws IOException
	 */
	public int[][] readFileMatrix(String filename,int column,int row) throws IOException{
		int [][] matrix = new int[row][column];
		File fi = new File (filename);
		 FileInputStream fin = new FileInputStream(fi);
		 BufferedReader br; br = new BufferedReader( new InputStreamReader( fin ) );
         String s = new String();
         for(int i = 0; i < row; i++)
         {
             s = br.readLine().trim();
             String[] B = s.split(" ");
             for(int j = 0; j < column; j++)
             {
                 matrix[i][j] = new Integer(B[j].trim());
             }
         }     
         return matrix;
	}
	
	/**
	 * Tim so file,thu muc trong 1 folder cho truoc
	 * @param foldername
	 * @return list file of folder
	 */
	public String[] readListFile(String foldername){
		File file = new File(foldername);
		String [] list = file.list();
		return list;
	}
}
